package com.example.administrator.qimo3no74.goods;


import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.qimo3no74.R;
import com.robertlevonyan.views.chip.Chip;
import com.robertlevonyan.views.chip.OnChipClickListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsFragment extends Fragment implements OnBannerListener {
    private Banner mBanner;
    private ArrayList<Integer> imagePath;
    private ArrayList<String> imageTitle;
    private MyImageLoader mMyImageLoader;
    private Chip goodsChipShare;
    private TextView goodsTextViewOldPrice;
    private TextView goodsTextViewStoreName;
    private ImageView goodsImageViewStore;
    private TextView goodsTextViewAllComment;



    public GoodsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_goods, container, false);

        //初始化
        goodsChipShare = view.findViewById(R.id.goodsChipShare);
        goodsTextViewOldPrice = view.findViewById(R.id.goodsTextViewOldPrice);
        goodsTextViewStoreName = view.findViewById(R.id.goodsTextViewStoreName);
        goodsImageViewStore = view.findViewById(R.id.goodsImageViewStore);
        goodsTextViewAllComment = view.findViewById(R.id.goodsTextViewAllComment);

        //原价加删除线
        goodsTextViewOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );

        //点击事件
        goodsOnClick();



         //轮播图
        initData();
        initView(view);

        return view;
    }

    private void goodsOnClick() {
        //分享
        goodsChipShare.setOnChipClickListener(new OnChipClickListener() {
            @Override
            public void onChipClick(View v) {
                Toast.makeText(v.getContext(), "分享给你好友哦", Toast.LENGTH_SHORT).show();
            }
        });

        //店铺
        goodsTextViewStoreName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), goodsTextViewStoreName.getText()+" 店铺里还有更多哦", Toast.LENGTH_SHORT).show();
            }
        });
        goodsImageViewStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), goodsTextViewStoreName.getText()+" 店铺里还有更多哦", Toast.LENGTH_SHORT).show();
            }
        });

        //评论
        goodsTextViewAllComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "评论区还有很多精彩评论", Toast.LENGTH_SHORT).show();
            }
        });


    }

    private void initData() {
        imagePath = new ArrayList<>();
        imagePath.add(R.drawable.beixin_qun1);
        imagePath.add(R.drawable.beixin_qun2);
        imagePath.add(R.drawable.beixin_qun3);
        imagePath.add(R.drawable.beixin_qun4);
    }

    private void initView(View view) {
        mMyImageLoader = new MyImageLoader();
        mBanner = view.findViewById(R.id.banner);
        //设置样式，里面有很多种样式可以自己都看看效果
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        mBanner.setImageLoader(mMyImageLoader);
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        mBanner.setBannerAnimation(Transformer.Default);
        //mBanner.setBannerAnimation(Transformer.ZoomOutSlide);
        //轮播图片的文字
        mBanner.setBannerTitles(imageTitle);
        //设置轮播间隔时间
        mBanner.setDelayTime(10000);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        mBanner.setImages(imagePath)
                //轮播图的监听
                .setOnBannerListener(this)
                //开始调用的方法，启动轮播图。
                .start();

    }

    /**
     * 轮播图的监听
     *
     * @param position
     */
    @Override
    public void OnBannerClick(int position) {
        Toast.makeText(getContext(), "你点了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
    }


    /**
     * 图片加载类
     */
    private class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }

}
